package com.js.myapp.system.activity;

// Created by JS on 2021/2/1.

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.js.basicpage.activity.BaseLoadingDialogActivity;
import com.js.basicutils.NavigationBarUtil;
import com.js.basicutils.PrinterUtil;
import com.js.basicutils.StatusBarUtil;
import com.js.basicutils.ToastUtils;
import com.js.myapp.R;
import com.js.myapp.common.activity.PrinterConnectActivity;
import com.qs.helper.printer.PrintService;
import com.qs.helper.printer.PrinterClass;

import java.io.UnsupportedEncodingException;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PrintActivity extends BaseLoadingDialogActivity {

    @BindView(R.id.back_text)
    TextView backText;
    @BindView(R.id.header_back)
    LinearLayout headerBack;
    @BindView(R.id.centre_title)
    TextView centreTitle;
    @BindView(R.id.right_title)
    TextView rightTitle;
    @BindView(R.id.header)
    RelativeLayout header;
    @BindView(R.id.btn_print_qr_code)
    Button btnPrintQrCode;
    @BindView(R.id.btn_print_text)
    Button btnPrintText;
    @BindView(R.id.btn_print_qr_code_text)
    Button btnPrintQrCodeText;

    private PrintActivity mContext;

    public static void start(Context context) {
        Intent intent = new Intent(context, PrintActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        setContentView(R.layout.activity_print);
        ButterKnife.bind(this);
        StatusBarUtil.setStatusBarColor(mContext, R.color.color_statusbar_bg);
        NavigationBarUtil.setSysNavigationColor(mContext, Color.parseColor("#141d26"));
        initView();
    }

    private void initView() {
        centreTitle.setVisibility(View.GONE);
        rightTitle.setText("连接打印机");
    }

    @OnClick({R.id.header_back, R.id.right_title, R.id.btn_print_qr_code, R.id.btn_print_text,
            R.id.btn_print_qr_code_text})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.header_back:
                finish();
                break;
            case R.id.right_title:
                PrinterConnectActivity.start(mContext);
                break;
            case R.id.btn_print_qr_code:
                printQrCode();
                break;
            case R.id.btn_print_text:
                printText();
                break;
            case R.id.btn_print_qr_code_text:
                printQrCodeAndText();
                break;
        }
    }

    /**
     * 打印二维码
     */
    private void printQrCode() {
        if (!checkPrinterConnecting()) return;
        for (int i = 0; i < 3; i++) {
            PrintService.pl.write(PrinterUtil.ALIGN_CENTER); // 设置中间对齐
            Bitmap bitmap = PrinterUtil.createQRImage("Jorelanbo", 150, 150);
            if (bitmap != null) {
                PrintService.pl.write(PrinterUtil.draw2PxPoint(bitmap));
                try {
                    PrintService.pl.write(PrinterUtil.getTestBytes(" \n"));
                    PrintService.pl.write(PrinterUtil.getTestBytes(" \n"));
                    PrintService.pl.write(PrinterUtil.getTestBytes(" \n"));
                    PrintService.pl.write(PrinterUtil.getTestBytes(" \n"));
                    PrintService.pl.write(PrinterUtil.getTestBytes(" \n"));
                    PrintService.pl.write(PrinterUtil.getTestBytes(" \n"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                PrintService.pl.write(PrinterUtil.RESET);
            }
        }
    }

    /**
     * 打印文字
     */
    private void printText() {
        if (!checkPrinterConnecting()) return;
        try {
            PrintService.pl.write(new byte[]{0x1D, 0x21, 0x11}); // 设置设置字符大小
            PrintService.pl.write(new byte[]{0x1b, 0x33, 0x0f}); // 设置行间距 最后一位为16进制的行距值
            PrintService.pl.write(PrinterUtil.ALIGN_LEFT); // 设置左对齐
            PrintService.pl.write(PrinterUtil.getTestBytes("Jorelanbo \n"));
            PrintService.pl.write(PrinterUtil.getTestBytes(" \n"));
            PrintService.pl.write(PrinterUtil.getTestBytes(" \n"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        PrintService.pl.write(PrinterUtil.RESET);
    }

    /**
     * 打印图片和文字
     */
    private void printQrCodeAndText() {
        if (!checkPrinterConnecting()) return;
        PrintService.pl.write(PrinterUtil.ALIGN_CENTER); // 设置中间对齐
        Bitmap bitmap = PrinterUtil.createQRImage("Jorelanbo", 150, 150);
        Bitmap newBitmap = PrinterUtil.addWatermark(bitmap, 450, 150, "Jorelanbo");
        if (newBitmap != null) {
            PrintService.pl.write(PrinterUtil.draw2PxPoint(newBitmap));
            try {
                PrintService.pl.write(PrinterUtil.getTestBytes(" \n"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        PrintService.pl.write(PrinterUtil.RESET);
    }

    /**
     * 检查打印机连接
     * @return
     */
    private boolean checkPrinterConnecting() {
        if (PrintService.pl == null) {
            ToastUtils.showToast(mContext, "请连接打印机");
            return false;
        }

        if (PrintService.pl.getState() != PrinterClass.STATE_CONNECTED) {
            ToastUtils.showToast(mContext, "打印机未连接，请先连接打印机！");
            return false;
        }
        return true;
    }
}
